package com.mq.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mq.common.Consumer;
import com.mq.common.ConsumerEnv;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/*
* 这个类表示一个存储消息的队列
* MSG => Message
*/
public class MSGQueue {
    //表示队列的身份标识
    private String name;
    //表示队列是否持久化，true表示永久保存
    private boolean durable = false;
    //true表示这个队列只能被一个消费者使用,没有实现
    private boolean exclusive = false;
    //true表示没有消费者使用之后，自动删除。没有实现
    private boolean autoDelete = false;
    //添加扩展参数，没有实现
    private Map<String,Object> arguments = new HashMap<>();
    //当前队伍有哪些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    //记录当前取到了第几个消费者，方便实现轮询
    private AtomicInteger consumerSeq = new AtomicInteger(0);//原子操作

    //添加一个新的订阅者
    public void addConsunerEvn(ConsumerEnv consumerEnv){
            consumerEnvList.add(consumerEnv);
    }
    //订阅者的删除暂时不考虑
    //挑一个新的订阅者用来处理当前的消息（按照轮询的方式）
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvList.size() == 0){
            return null;
        }
        //计算一下当前要取得元素下标
        int index = consumerSeq.get() % consumerEnvList.size();//轮询
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

   /* public Map<String, Object> getArgument() {
        return arguments ;
    }*/
    public String getArguments(){
        //Argument参数，Map--->String(JSON),数据库拿String格式存储
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            //代码异常，返回一个空JSON
            return "{}";
        }
    }

   /* public void setArgument(Map<String, Object> arguments) {
        this.arguments = arguments;
    }*/
    public void setArguments(String argumentsJson){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            //readValue用来表述房钱的json字符串要转化成的java对象是啥类型的，使用匿名复杂对象描述Map
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    public void setArguments(Map<String,Object> arguements){
        this.arguments = arguements;
    }
    //把当前数据库拿出来的String类型Argument,<HashMap<String,Object>>(){})
    //用于测试
    public Object getArgumentValue(String key) {
        return arguments.get(key);
    }

    public void setArgumentMap(HashMap<String, Object> hashMap) {
        this.arguments = hashMap;
    }
}
